package top.jsjkxyjs.blackbox.common.interceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import top.jsjkxyjs.blackbox.entity.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class LoginInterceptor implements HandlerInterceptor {
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		// 获取session，判断session用户是否存在信息

		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			String urlString = request.getRequestURI();
			if (urlString.endsWith("login")) {
				return true;
			}
			response.sendRedirect(request.getContextPath() + "/login");
		}
		return true;
	}
}
